/**
 * 
 */
package cm.dpassyann.speedDating.model.user.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Service;

import cm.dpassyann.speedDating.model.user.UserConstant;
import cm.dpassyann.speedDating.model.user.bean.User;
import cm.dpassyann.speedDating.model.user.dao.IUserDao;

/**
 * 
 * Pour le projet : AmourMeZam
 * @author deungoueyann
 * @description cette classe permet d'implémenter les différentes méthode CRUD pour une 
 * entité
 * @date fait le : 20 nov. 2012
 */
@Service
public class UserDaoImpl implements IUserDao{

	@SuppressWarnings("deprecation")
	private SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();


	@Override
	public Long addUser(User user) {

		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		session.close();

		return user.getId();
	}


	@Override
	public List<User> getAll() {

		// Retrieve session from Hibernate
		Session session = sessionFactory.openSession();
		// Create a Hibernate query (HQL)
		//Query query = session.createQuery("FROM  User");
		Query query = session.getNamedQuery(UserConstant.USER_GET_ALL_QUERY);

		// Retrieve all
		@SuppressWarnings("unchecked")
		List<User> users = (ArrayList<User>)query.list();
		session.close();

		return  users;
	}

	@Override
	public User get(Long id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.openSession();
		// Retrieve existing person first
		User user = (User) session.get(User.class, id);

		session.close();
		return user;
	}


	@Override
	public User isExist(String login, String password) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.openSession();

		Query query = session.getNamedQuery(UserConstant.USER_CONNECTION_QUERY)
				.setString("username", login).setString("password", password);
		@SuppressWarnings({ "unchecked" })
		List<User> users =  (ArrayList<User>)query.list();
		
		if(users != null && users.size()==1){
			return users.get(0);
		}
		
		return null;
	}
	/*	
	public static void main (String [] arg){

		Address adr = new Address();
		Address adr2 = new Address();
		User u1 = new User();
		User u2 = new User();
		
		ConnectionParameter p1 = new ConnectionParameter();
		ConnectionParameter p2 = new ConnectionParameter();
		
		p1.setPassword("Nganknoella89");
		p1.setUsername("titi");
		
		p2.setPassword("deungoue");
		p2.setUsername("toto");

		u1.setAddress(adr);
		u1.setBirthday(new Date());
		u1.setEmail("titi@gmail.com");
		u1.setActived(false);
		u1.setParameter(p1);

		adr2.setCity("brest");
		adr2.setComments("");
		adr2.setNumeroRue(5);
		adr2.setStreet("Rue du commandant Drogou");
		adr2.setZipCode(29200);
		

		u2.setAddress(adr2);
		u2.setBirthday(new Date());
		u2.setEmail("toto@gmail.com");
		u2.setActived(false);
		u2.setParameter(p2);


		UserDaoImpl daoImpl = new UserDaoImpl();
		daoImpl.addUser(u1);
		daoImpl.addUser(u2);
		ArrayList<User> users = (ArrayList<User>)daoImpl.getAll();
		for(User user : users){
			System.out.println(user);
		}

		/*try {
			Socket client = new Socket("127.0.0.1", 4444);
			PrintWriter writer = new PrintWriter(client.getOutputStream(), true);

			String message = "Hello Yann Chou\n";

			writer.write(message);
			writer.flush();
			writer.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
//*/

}
